// pages/list/list.js
Page({

  data: {
    price:0,
    nam:0,
    checkedarr:false,
    myadd:[]
  },
  onShow(){
    this.mylist()
  },
  mylist(){
    this.setData({
      myadd:wx.getStorageSync('go')||[],
    })

    let index=this.data.myadd.findIndex(item=>{
      return item.checked==false
    })

    if(index>-1){
      this.setData({
        checkedarr:false

      })
    }else{
      this.setData({
        checkedarr:true
      })
    }

    let  _num=0
    let _price=0
    this.data.myadd.forEach(item=>{
      if(item.checked){
        _num+=item.num
        _price+=item.num*item.price
      }
    })  
    this.setData({
      num: _num,
      price: _price
    })

  },

  onstep(e){
    console.log(e)
    let arr = this.data.myadd
    arr[e.currentTarget.dataset.index].num = e.detail
    wx.setStorageSync('go', arr) 
    this.mylist()
  },

  onSing(e){
    console.log(e);
    let arr = this.data.myadd
    arr[e.currentTarget.dataset.index].checked = !arr[e.currentTarget.dataset.index].checked

    wx.setStorageSync('go', arr)
    this.mylist()

  },

  onChange(){
    let arr = this.data.myadd
    arr.forEach(item=>{
      item.checked = !this.data.checkedarr
    })
    wx.setStorageSync('go', arr)
    this.mylist()
  },

  sanchu(e){
   let arr=this.data.myadd
   arr.splice(e.currentTarget.dataset.index,1)
   wx.setStorageSync('go', arr)
   this.mylist()

  }

  
})